"""Setup the catalogue application"""
import logging

import pylons.test

from chowa.lib.site.auth import add_permissions, add_groups
from catalogue.config.environment import load_environment
from catalogue.model.meta import Session, Base

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup catalogue here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    Base.metadata.create_all(bind=Session.bind)
    
    # Add permissions and groups
    admin_perms = [
        'catalogue.administer.sections',
        'catalogue.administer.tags',
        'catalogue.administer.types',
        'catalogue.administer.documents']
    author_perms = [
        'catalogue.add.document',
        'catalogue.edit.document',
        'catalogue.delete.document']
    
    add_permissions(admin_perms + author_perms, Session)
    add_groups([
        ('catalogue.administrators', admin_perms),
        ('catalogue.content_creators', author_perms),
    ], Session)
